﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace ERPStore.Web.ModelMetadata
{
	public class ModelMetaDataBuilder<TModel,TProperty>
	{
		private ModelMetaData m_Item;

		public ModelMetaDataBuilder(ModelMetaData item)
		{
			m_Item = item;
		}

		public ModelMetaDataBuilder<TModel, TProperty> Required(bool allowEmptyString = false, string errorMessage = null, string errorMessageResourceName = null, string errorMessageResourceType = null)
		{
			m_Item.Required = new Required();
			m_Item.Required.AllowEmptyString = allowEmptyString;
			m_Item.Required.ErrorMessage = errorMessage;
			m_Item.Required.ErrorMessageResourceName = errorMessageResourceName;
			m_Item.Required.ErrorMessageResourceType = errorMessageResourceType;
			return this;
		}

		public ModelMetaDataBuilder<TModel, TProperty> RequiredWhen(Func<TModel, bool> condition)
		{
			m_Item.Required = new Required();
			var type = typeof(TModel);
			Func<object, bool> nonGenericCondition = x => condition((TModel)x);
			m_Item.Required.Condition = nonGenericCondition;
			return this;
		}

		public ModelMetaDataBuilder<TModel, TProperty> DisplayFormatString(string value)
		{
			m_Item.DisplayFormatString = value;
			return this;
		}

		public ModelMetaDataBuilder<TModel, TProperty> Description(string description)
		{
			m_Item.Description = description;
			return this;
		}

		public ModelMetaDataBuilder<TModel, TProperty> Watermark(string watermark)
		{
			m_Item.Watermark = watermark;
			return this;
		}

		public ModelMetaDataBuilder<TModel, TProperty> DisplayName(string displayName)
		{
			m_Item.DisplayName = displayName;
			return this;
		}

		public ModelMetaDataBuilder<TModel, TProperty> MaxStringLength(int length)
		{
			m_Item.MaxStringLength = length;
			return this;
		}

		public ModelMetaDataBuilder<TModel, TProperty> MinStringLength(int length)
		{
			m_Item.MinStringLength = length;
			return this;
		}

	}
}
